<?php
class AppController extends Controller {
	var $components = array('RequestHandler', 'Cookie');
	var $helpers = array('html', 'javascript', 'form', 'ajax', 'ical');
	var $uses = array('Setting');
	
	function beforeFilter(){
		//lokalizálás
		$this->_setLanguage();
		
		App::import('Sanitize');
		$this->setGeneralData();
	}
	
	function setGeneralData(){
		$this->settings = $this->Setting->find('list', array('fields' => array('Setting.name', 'Setting.value')));
		$v = $this->getVersions();
		$this->settings['version'] = $v['actual'];
		$this->set('settings',$this->settings);
		
		//vendor('calendar','task_calendar', 'json_php4');
		App::import('vendor', 'calendar');
		App::import('vendor', 'task_calendar');
		App::import('vendor', 'json_php4');
		
		//php4-hez támogatást adó eljárás
		if(!function_exists('json_encode')){
			$GLOBALS['JSON_OBJECT'] = new Services_JSON();
			function json_encode($value){
				return $GLOBALS['JSON_OBJECT']->encode($value);
				}
			
			function json_decode($value){
				return $GLOBALS['JSON_OBJECT']->decode($value);
				}
			}
		
		$cal = new TaskCalendar;
		$cal->setMonthNames(array(
										__('January', true),
										__('February', true),
										__('March', true),
										__('April', true),
										__('May', true),
										__('June', true),
										__('July', true),
										__('August', true),
										__('September', true),
										__('October', true),
										__('November', true),
										__('December', true)
										)
								  );
		$cal->setDayNames(array(
										__('Sunday', true),
										__('Monday', true),
										__('Tuesday', true),
										__('Wednesday', true),
										__('Thursday', true),
										__('Friday', true),
										__('Saturday', true),
										)
								);
		$cal->startDay = 1;	//JavaScript mitt muszáj így maradnia, localizálásnál változtatni kell
		$this->set('cal', $cal);
		
		$ma['sql'] = date('Y-m-d');
		$this->set('ma',$ma);
		
		define('UPDATEURL', 'http://code.google.com/p/what2do/downloads/list'); //ha módosul a js-ben is módosítani kell
		//css definíciók ha valami gubanc van érdemes szétnézni, hogy nem változott-e valami a css fileban
		define('CSS_TASK_FINISHED', 'closed');
		define('CSS_TASK_DELETED', 'deleted');
		define('CSS_TASK_OVERDUED', 'overdued');
		define('CSS_HIDDEN', 'rejtett');

		/*debug*/
		if(isset($_GET['showbid']) && $_GET['showbid']==1) define('SHOWBIDJID',true);
		else define('SHOWBIDJID',false);
		}
		
	/**
	* annak ellenőrzése, hogy a user be van-e jelentkezve
	*
	* @return semmi
	* @access public
	*/
	function belepesCheck(){
		if(!$this->Session->check('User') && $this->params['url']['url'] != 'tasks/getIcal/categories_id=5.ics'){		
			//user beléptetése
			$this->redirect('/users/login');
			exit();
			}
		}
		
	function getVersions(){
		//verzióinformációk lekérése
		//3 naponta ellenőrizzük egyszer
		$lastCheck = $this->Setting->find('first', array('conditions' => array('name' => 'last_version_check')));
		//a telepített verzió a version-info fájlban
		$w2dver['actual'] = trim(str_replace('what2Do: ', '', file_get_contents('../../version-info')));
		//az elérhető legfrissebb verzió a weben
		if((time() - $lastCheck['Setting']['value']) > 60*60*24*3){
			$w2dver['recent'] = trim(str_replace('what2Do: ', '', @file_get_contents('http://webmania.cc/w2d/version-info')));
			$this->Setting->query('UPDATE '.$this->Task->tablePrefix.'settings
												SET `value` = UNIX_TIMESTAMP( )
												WHERE name = "last_version_check"');
		}
		else
			$w2dver['recent'] = $w2dver['actual'];
		return($w2dver);
		}
		
	function secuCheck(){
		//általános biztonsági ellenőrzések
		
		//install.php olvasható?
		$installFile = is_readable('../../install.php');
		$security['installFile'] = $installFile;
		
		return($security);
	}
	
/*
* Lokalizálás beállítása
*/
	function _setLanguage(){
		App::import('L10n');

		if($this->Cookie->read('what2Do_lang') && !$this->Session->check('Config.language')){
			$this->Session->write('Config.language', $this->Cookie->read('what2Do_lang'));
		}
		elseif(isset($this->params['language']) && ($this->params['language'] !=  $this->Session->read('Config.language'))){
			$this->Session->write('Config.language', $this->params['language']);
			$this->Cookie->write('what2Do_lang', $this->params['language'], null, '365 days');
		}
      
		$this->L10n = new L10n();
		$this->L10n->get($this->Session->read('Config.language'));
		Configure::write('Config.language', $this->Session->read('Config.language'));
		$this->L10n->get(Configure::read('Config.language'));
   }

}
?>
